/**
 * 
 */
package kr.co.insoft.core.repository.configration.connection.impl;

import java.util.List;

import kr.co.insoft.core.mapper.configration.connection.ConnectionMapper;
import kr.co.insoft.core.model.configration.connection.ConnectionEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.repository.configration.connection.ConnectionRepository;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author skpark
 * 
 */
@Repository
public class ConnectionRepositoryImpl implements ConnectionRepository {

	@Autowired
	ConnectionMapper connectionMapper;

	@Override
	public List<ConnectionEntityModel> getLists(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
			Assert.notNull(pagingModel.getCurrentPage());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return connectionMapper.getLists(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public int getTotalCount(PagingModel pagingModel)
			throws RepositoryException {
		try {
			Assert.notNull(pagingModel);
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return connectionMapper.getTotalCount(pagingModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void createConnection(ConnectionEntityModel connectionEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(connectionEntityModel);
			Assert.notNull(connectionEntityModel.getConnectionName());
			Assert.notNull(connectionEntityModel.getVenderid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			connectionEntityModel.setConnectionid(connectionMapper.getUuid());
			connectionMapper.createConnection(connectionEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public ConnectionEntityModel getConnection(
			ConnectionEntityModel connectionEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(connectionEntityModel);
			Assert.notNull(connectionEntityModel.getConnectionid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return connectionMapper.getConnection(connectionEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public void removeConnection(ConnectionEntityModel connectionEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(connectionEntityModel);
			Assert.notNull(connectionEntityModel.getConnectionid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			connectionMapper.removeConnection(connectionEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	@Override
	public List<ConnectionEntityModel> getAllLists(
			ConnectionEntityModel connectionEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(connectionEntityModel);
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return connectionMapper.getAllLists(connectionEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
